package net.trombonium.steroid;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

public class Notify extends BroadcastReceiver {
	public static final String S_ID = "net.trombonium.steroid.survey_id";
	private static final String TAG = "Notify";
	
	public static void setSurveyNotification(Context context, int surveyId){
		setSurveyNotification(context, surveyId, System.currentTimeMillis());
	}
    
    public static void setSurveyNotification(Context context, int surveyId, long time){
    	String surveyName = (new DBHelper(context)).getSurveyName(surveyId);
    	
        Notification notification = new Notification(R.drawable.tamu, "New Survey - "+surveyName, time);
		
        Intent steroidIntent = new Intent(context, Surveyor.class);
		steroidIntent.putExtra(Steroid.S_ID, surveyId);
		steroidIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		PendingIntent pendingSteroid = PendingIntent.getActivity(context, 0, steroidIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
    	notification.setLatestEventInfo(context, "Steroid Survey", "Time to take survey: "+surveyName, pendingSteroid);
		
    	notification.defaults |= Notification.DEFAULT_SOUND;
		((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).notify(surveyId, notification);
    }
    
    public static void clearNotification(Context context, int surveyId){
    	((NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(surveyId);
    }

	@Override
	public void onReceive(Context context, Intent intent) {
		Log.i(TAG, "Caught an intent");
		int surveyId = intent.getIntExtra(Notify.S_ID, -1);
		if(surveyId != -1)
			setSurveyNotification(context, surveyId);
	}
}